<?php

/**
 * 
 * @author NutMotion <simonotm.linux@gmail.com> 
 */
require_once(dirname(__FILE__) . "/bookmarksCreate.php");
require_once(dirname(__FILE__) . "/../commons/common-funcs.php");
require_once(dirname(__FILE__) . "/../db/common-operations.php");
require_once(dirname(__FILE__) . "/../db/associations.php");
require_once(dirname(__FILE__) . "/../db/bookmarksOperations.php");
require_once(dirname(__FILE__) . "/../db/tagsOperations.php");

function handleDeleteByUrlAndTag($request) {
//format of the request: 
//bookmarks.php?action=delete&u=<url>&ta=<tag>
    
    //the ids of the url and tag in the db
    $webpageId;
    $tagId;
    
    //the parameters we've received
    $urlFromRequest=$request["u"];
    $tagFromRequest=$request["ta"];
    
    connect();
    
    //0)we retrieve the db identifiers for the url and the tag

    $arDbQueryResult = selectFromTable2("webpage", "id", "url='" . mysql_real_escape_string($urlFromRequest). "'","id");
    $webpageId=$arDbQueryResult[0]["id"];
    
    $arDbQueryResult = selectFromTable2("tags", "id", "tag='" . mysql_real_escape_string($tagFromRequest)."'","id");
    $tagId=$arDbQueryResult[0]["id"];    

        
    //1)delete the pair URL,Tag from table webpage_has_tags
    if (deleteFromTable("webpage_has_tags","webpage_id=$webpageId AND tags_id=$tagId"))
            $success=true;
    
    //2)check whether the tag is still associated with a webpage. 
    //if not, we delete the tag
    
    //2a) is the tagId still present in the webpage_has_tags table ?
    $arDbQueryResult = selectFromTable2("webpage_has_tags", "id", "tags_id=$tagId","id");
        
    //2b) if not, we delete the tag
    if (count($arDbQueryResult)==0) {
        deleteFromTable("tags","id=$tagId");
    }
    
    //3)check whether the webpage is still associated with a tag. 
    //if not, we delete the webpage
    
    //3a) is the webpageId still present in the webpage_has_tags table ?
    $arDbQueryResult = selectFromTable2("webpage_has_tags", "id", "webpage_id=$webpageId","id");
        
    //3b) if not, we delete the webpage
    if (count($arDbQueryResult)==0) {
        deleteFromTable("webpage","id=$webpageId");
    }
    
    
//    
//    /*******test
//     * 
//     */
//     $arDbQueryResult = selectFromTable2("webpage", "id", "url='" . mysql_real_escape_string($urlFromRequest). "'","id");
//    $webpageId=$arDbQueryResult[0]["id"];
//    
//    $arDbQueryResult = selectFromTable2("tags", "id", "tag='" . mysql_real_escape_string($tagFromRequest)."'","id");
//    $tagId=$arDbQueryResult[0]["id"];
//    
//    echo $urlId." ".$tagId;
    
    $arReturnResults=array();
    $arFeedbackMsg=array();
    
    if ($success) {
        $arReturnResults["default"]["tag"]=$tagFromRequest;
    }
        
    return new ResultContext($success, $arReturnResults, $arFeedbackMsg);

}

/*******************************TESTS
 * 
 */
//test1();

function test1() {
    //how we operate: we create a webpage, a tag, then we delete everything
    $testUrl="http://free.domain.name/en.html?domain=doesntexist.com";
    $testTag="dummyfortest1";

    $testRequest=array("url"=>$testUrl, "tags"=>array($testTag));
//    handleCreateOperation($testRequest);

    //ok, dummy page created, we can erase it
    //format of the request: 
    //bookmarks.php?action=delete&u=<url>&ta=<tag>
    $testRequest=array("u"=>$testUrl, "ta"=>$testTag);
    handleDeleteByUrlAndTag($testRequest);
}    
?>
